package com.jrmf.redenvelopes.order.util;

import java.beans.PropertyDescriptor;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtilsBean;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.junit.Assert;

public class BeanUtil {
    /**
     * 将javabean实体类转为map类型，然后返回一个map类型的值
     * FIXME 
     * @param obj
     * @return
     */
    public static Map<String, Object> beanToMap(Object obj) { 
            Map<String, Object> params = new HashMap<String, Object>(0); 
            try { 
                PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean(); 
                PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(obj); 
                for (int i = 0; i < descriptors.length; i++) { 
                    String name = descriptors[i].getName(); 
                    if (!"class".equals(name)) { 
                        params.put(name, propertyUtilsBean.getNestedProperty(obj, name)); 
                    } 
                } 
            } catch (Exception e) { 
                e.printStackTrace(); 
            } 
            return params; 
    }

    public static void main(String[] args) throws Throwable{
        long start = System.currentTimeMillis();
        SXSSFWorkbook wb = new SXSSFWorkbook(10000); // keep 100 rows in memory, exceeding rows will be flushed to disk
        Sheet sh = wb.createSheet();
        Row ro = sh.createRow(0);
        String[] titles ={"name1","name2","name3","name4","name5","name6","name7","name8","name9","name9","name10"};
        for(int cellnum = 0; cellnum < titles.length; cellnum++){
            Cell cell = ro.createCell(cellnum);
            cell.setCellValue(titles[cellnum]);
        }

        for(int rownum = 1; rownum < 500000; rownum++){
            Row row = sh.createRow(rownum);
            for(int cellnum = 0; cellnum < 10; cellnum++){
                Cell cell = row.createCell(cellnum);
                String address = new CellReference(cell).formatAsString();
                cell.setCellValue(address);
            }
        }

        // Rows with rownum < 900 are flushed and not accessible
        /*for(int rownum = 0; rownum < 900; rownum++){
            Assert.assertNull(sh.getRow(rownum));
        }*/

        // ther last 100 rows are still in memory
        /*for(int rownum = 900; rownum < 10000; rownum++){
            Assert.assertNotNull(sh.getRow(rownum));
        }*/

        FileOutputStream out = new FileOutputStream("C:\\develop\\xx.xlsx");
        wb.write(out);

        out.close();
        long millis = System.currentTimeMillis() - start;
        long second = millis / 1000;
        System.out.println(second+"s");

        // dispose of temporary files backing this workbook on disk
        wb.dispose();
    }
}
